package com.session05.am;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Calculate extends JFrame {

    private JTextField txtInput;
    private JButton one, two, three, four, five, six, seven, eight, nine, zero;
    private JButton division, multiplication, minus, adds, equals, points;
    private JPanel p1, p2;

    public Calculate() {
        this.setTitle("计算器");
        this.setBounds(500, 100, 300, 400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        p1 = new JPanel();
        txtInput = new JTextField();
        txtInput.setPreferredSize(new Dimension(280, 50));
        p1.add(txtInput);
        this.add(p1, BorderLayout.NORTH);

        p2 = new JPanel();
        seven = new JButton("7");
        eight = new JButton("8");
        nine = new JButton("9");
        division = new JButton("/");
        p2.add(seven);
        p2.add(eight);
        p2.add(nine);
        p2.add(division);

        four = new JButton("4");
        five = new JButton("5");
        six = new JButton("6");
        multiplication = new JButton("*");
        p2.add(four);
        p2.add(five);
        p2.add(six);
        p2.add(multiplication);

        one = new JButton("1");
        two = new JButton("2");
        three = new JButton("3");
        minus = new JButton("-");
        p2.add(one);
        p2.add(two);
        p2.add(three);
        p2.add(minus);

        zero = new JButton("0");
        points = new JButton(".");
        equals = new JButton("=");
        adds = new JButton("+");
        p2.add(zero);
        p2.add(points);
        p2.add(equals);
        p2.add(adds);

        p2.setLayout(new GridLayout(4, 4, 5, 5));
        this.add(p2);

        mouseEnter(one);
        mouseEnter(two);
        this.setVisible(true);
    }

    public void mouseEnter(JButton btn) {
        btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String str = btn.getText();
                Integer integer = Integer.valueOf(str);
                txtInput.setText(txtInput.getText() + str);
            }
        });
    }

    public static void main(String[] args) {
        Calculate calculate = new Calculate();
    }
}
